package com.ruoyi.project.product.series.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.series.mapper.SeriesMapper;
import com.ruoyi.project.system.post.domain.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/15
 * @Description: com.ruoyi.project.product.series.service
 * @version: 1.0
 */
@Service
public class SeriesServiceImpl implements SeriesService {

    @Autowired
    private SeriesMapper seriesMapper;

    @Override
    public List<Series> selectSeriesList(Series series) {
        return seriesMapper.selectSeriesList(series);
    }

    @Override
    public String checkSeriesNameUnique(Series series) {
        Long seriesId = StringUtils.isNull(series.getSeriesId()) ? -1L : series.getSeriesId();
        Series info = seriesMapper.checkSeriesNameUnique(series.getSeriesName());
        if (StringUtils.isNotNull(info) && info.getSeriesId().longValue() != seriesId.longValue())
        {
            return UserConstants.SERIES_NAME_NOT_UNIQUE;
        }
        return UserConstants.SERIES_NAME_UNIQUE;
    }

    @Override
    public int insertSeries(Series series) {
        series.setCreateBy(ShiroUtils.getLoginName());
        return seriesMapper.insertSeries(series);
    }

    @Override
    public Series selectSeriesById(Long seriesId) {
        return seriesMapper.selectSeriesById(seriesId);
    }

    @Override
    public int updateSeries(Series series) {
        return seriesMapper.updateSeries(series);
    }

    @Override
    public int deleteSeriesByIds(String ids) {
        Long[] seriesIds = Convert.toLongArray(ids);
        //应该在删除之前先校验该类型下是否有公告，有则不能删除，报错

        return seriesMapper.deleteSeriesByIds(seriesIds);
    }
}
